import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;


public class NetworkModule extends UnicastRemoteObject implements Service {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public final static String ServiceName = "tictactoe";
	public final static int ServicePort = 1095;

protected TicTacToeGui owner;
	protected TicTacToeGui client;


	public NetworkModule(TicTacToeGui owner) throws RemoteException {
		super();
		this.owner = owner;
		// TODO Auto-generated constructor stub
	}


	@Override
	public boolean placeMark(int row, int col, char mark)
			throws RemoteException {
		// TODO Auto-generated method stub
		System.out.println("Placemark");
		owner.setMyTurn();
		owner.setMark(row, col, mark);
		owner.guiMessage("Your turn");
		return false;
	}


	@Override
	public void registerClient(Service client) throws RemoteException {
		// TODO Auto-generated method stub
		owner.setRemoteBoard(client);
		owner.guiMessage("Client connected");
	}


	@Override
	public void sendMsg(String msg) throws RemoteException {
		// TODO Auto-generated method stub
		owner.guiMessage(msg);
	}


	@Override
	public void resetGame() throws RemoteException {
		owner.clearBoard();
	}


	@Override
	public void gameOver() throws RemoteException {
		owner.gameOver();
	}


//	public static void main(String[] args) 
//	{
//		System.setSecurityManager( new LiberalSecurityManager() );
//		System.out.println("Initialize TicTacToeService...");
//		try
//		{
//			LocateRegistry.createRegistry(1095);
//			Service cserv = new NetworkModule();
//			String serverObjectName = "rmi://localhost:" + ServicePort
//					+ "/" + ServiceName;
//			Naming.rebind(serverObjectName, cserv);
//			System.out.println("Service is running.");
//		}
//		catch (Exception e)
//		{
//			System.out.println("Exception: " + e.getMessage());
//			e.printStackTrace();
//		}
//	}

}
